<?php
/**
 * Object Relational Persistence Mapping Class for table: pha_ewh_imticket
 *
 * @package	modules.dept_pharmacy.ordo
 * @author	viethd
 * @version $
 * @copyright 2009 LHV Company
 */
class PhaEwhImticket extends ORDataObject {

    /**#@+
     * Fields of table: pha_ewh_imticket mapped to class members
     */
    var $pha_ewh_imticket_id		= '';
    var $imticket_code		= '';
    var $pha_provider_id		= '';
    var $date		= '';
    var $input_user_id		= '';
    var $vat		= '';
    var $note		= '';
    var $enum_item_type		= '';
    var $run_out		= '';
    var $valuated        = '';
    var $finalized        = '';
    
    /**#@-*/


    /**
     * DB Table
     */
    var $_table = 'pha_ewh_imticket';

    /**
     * Primary Key
     */
    var $_key = 'pha_ewh_imticket_id';

    /**
     * Internal Name
     */
    var $_internalName = 'PhaEwhImticket';

    /**
     * Handle instantiation
     */
    function __construct() {
        parent::ORDataObject();
    }
    /**
     * generate import ticket code
     * [PN][YYYYMMDD + 3 digits]
     * @return [PN][YYYYMMDD + 3 digits]
     */
    public function generateImportTicketCode() {
        $phaSequencesObj = Celini::newOrdo('PhaSequences');
        $seq = $phaSequencesObj->getImTicketSeq();
        //gen code
        $today = date("Ymd");
        $formatted = "PN" . $today . sprintf("%06s", $seq);
        return 	 $formatted;
    }

    public function createImTicket($data = array()) {
        $result = array('id'=>'', 'code' => '');
        $this->populateArray($data);
        $code = $this->generateImportTicketCode();
        $this->imticket_code = $code;
        $this->persist();
        $result['id'] = $this->get_id();
        $result['code'] = $code;
        return 	$result;
    }

    public function setData($data = array()) {
        if (is_array($data)) {
            $this->populateArray($data);
        }
    }

    public function isExist($imTicketId) {
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "pha_ewh_imticket_id",
            'from'  => "$this->_table",
            'where' => "pha_ewh_imticket_id='$imTicketId'"
            
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
            return false;
        }
        return true;
    }
    
    public function searchImticket($filter = array()) {
        $condition = $this->buildSearchImTicketCondition($filter);
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "pha_ewh_imticket_id, imticket_code,
                        t.pha_provider_id, pp.name provider_name, date, input_user_id, p.name person_name, vat, note, enum_item_type, run_out",        
            'from'  => "$this->_table t LEFT JOIN pha_provider pp ON (t.pha_provider_id= pp.pha_provider_id) 
                        LEFT JOIN person p ON (t.input_user_id=p.person_id)",
            'where' => $condition
            
        );
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        return $result;
    } 
    
    public function setValuated($imTicketId) {        
        $sql = "UPDATE $this->_table
        SET valuated = 1
        WHERE pha_ewh_imticket_id = '$imTicketId'";
        $this->_execute($sql);        
        
    }
    
    private function buildSearchImTicketCondition($filterData = array()) {
        
        $sql = true;
        if (isset($filterData['imTicketCode']) &&  trim($filterData['imTicketCode']) !== "") {
            $sql = $sql. " AND (imticket_code LIKE '%".trim($filterData['imTicketCode'])."%')";
        }
        if (isset($filterData['fromDate']) &&  $filterData['fromDate'] !== "") {
            $sql = $sql. " AND date >= '" . $filterData['fromDate'] . "'";
        }
        if (isset($filterData['toDate']) &&  $filterData['toDate'] !== "") {
            $sql = $sql. " AND date <= '" . $filterData['toDate'] . "'";
        }
        if (isset($filterData['valuated']) && $filterData['valuated'] !== "") {
            $sql = $sql. " AND valuated = '" . $filterData['valuated'] . "'";
        }
        
        if (isset($filterData['finalized']) && $filterData['finalized'] !== "") {
            $sql = $sql. " AND finalized = '" . $filterData['finalized'] . "'";
        }
        return $sql;       
    }
    
    public function deleteImticket($imTicketId) {
        //delete all item of this import ticket
        $sql = "DELETE from pha_ewh_imticket_detail WHERE pha_ewh_imticket_id='$imTicketId'";
        $this->_execute($sql);
        //delete this import ticket
        $sql = "DELETE from $this->_table where pha_ewh_imticket_id='$imTicketId'";
        $this->_execute($sql);
    }
    
    public function canFinalize($imTicketId) {
        $ds =& new Datasource_sql();
        $sql = array(
            'cols'  => "DISTINCT t.pha_ewh_imticket_id",        
            'from'  => "$this->_table t INNER JOIN pha_ewh_imticket_detail peid ON (t.pha_ewh_imticket_id=$imTicketId AND t.pha_ewh_imticket_id= peid.pha_ewh_imticket_id AND peid.allow_sell=0)"
        );        
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();        
        if (empty($result)) {            
            return true;
        }
        //not empty mean there are some item not allow to sell
        return false;
    }
    
    public function finalize($imTicketId) {
        $sql = "UPDATE $this->_table SET finalized=1 WHERE pha_ewh_imticket_id='" . $imTicketId ."'" ;
        $this->_execute($sql);   
    }

}
?>
